# -*- coding: utf-8 -*-
#aula6_d
'''ex1
import urllib2
import time

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
             "http://ibm.com", "http://apple.com"]

start = time.time()
for host in hosts:
    url = urllib2.urlopen(host)
    print url.read(1024)
print "Elapsed Time: %s" %(time.time() - start)
'''
import Queue
import threading
import urllib2
import time

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
             "http://ibm.com", "http://apple.com"]
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue=queue

    def run(self):
        while True:
            host = self.queue.get()
            url = urllib2.urlopen(host)
            print url.read(1024)
            self.queue.task_done()

start = time.time()
def main():
    for i in range(3):
        t=ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    for host in hosts:
        queue.put(host)
    queue.join()
main()
print "Elapsed Time: %s" %(time.time() - start)
